package com.wfh.mianshiji.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

/**
 * @Title: DatasourceConfig
 * @Author wangfenghuan
 * @Package com.wfh.mianshiji.config
 * @Date 2025/11/3 14:28
 * @description:
 */
@Configuration
public class DatasourceConfig {

    @Bean
    @Primary
    public DataSource mysqlDatasource(MysqlDatasourceConfig config){
        return DataSourceBuilder
                .create()
                .url(config.getUrl())
                .username(config.getUsername())
                .password(config.getPassword())
                .driverClassName(config.getDriverClassName())
                .type(com.zaxxer.hikari.HikariDataSource.class)
                .build();
    }

    @Bean
    public DataSource postgresDataSource(PostgresDataSourceConfig props) {
        return DataSourceBuilder.create()
                .url(props.getUrl())
                .username(props.getUsername())
                .password(props.getPassword())
                .driverClassName(props.getDriverClassName())
                .type(com.zaxxer.hikari.HikariDataSource.class)
                .build();
    }

    @Bean
    public JdbcTemplate postgresJdbcTemplate(@Qualifier("postgresDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
}
